﻿<#
    Script  : ImportPSModule
    Author  : Riwut Libinuko
    Blog    : http://blog.libinuko.com 
    Copyright :© IdeasFree - cakriwut@gmail.com, 2013. All rights reserved.
#>


function Update-Profile
{
   param (
      [string] $profilePath,
      [string] $PSModuleName,
      [string] $PSModuleVer,
      $content
   )

   if(!(test-path $profilePath))
   {
      new-item -type file -path $profilePath -force | Out-Null
   }
   if($PSModuleVer)
   {
      $PSModuleVer = ".$PSModuleVer"
   }
   
   $profileContent = gc $profilePath
   if($profilecontent |? { $_ -match "#PSMODULE\:$PSModuleName(\.\d)*$" } )
   {
      Write-Host "Modifying existing profile entries"
      $profileContent |% {
        if($_ -match "\#PSMODULE\:$PSModuleName(\.\d)*$")
        {
            $contentStart = $true
            "`n#PSMODULE:$PSModuleName$PSModuleVer"
            $content |% {
               "Import-Module -name `"$($_.ModulePath)`""
            }            
        } elseif(($_ -match "\#PSMODULE") -and $contentStart)
        {
            $contentStart = $false
            "$_`n"
        } elseif($contentStart)
        {
           #Remove existing entries in the block
        } else {
            if($_) { 
               "$_"
            }
        }
      } | Set-Content $profilePath
      
   } else {
      
      $newScript = "`n#PSMODULE:$PSModuleName$PSModuleVer" 
      $newScript += $content |% {
             "`nImport-Module -name `"$($_.ModulePath)`""
          } 
      $newScript += "`n#PSMODULE" 
      
      $newScript | Add-Content $profilePath
   }
   
}


function Remove-Profile
{
    param (
      [string] $profilePath,
      [string] $PSModuleName
    )
	Write-Host "Processing $profilePath..." -NoNewline
    
    if(!(test-path $profilePath))
    {       
	   Write-Host "Skipped." -ForegroundColor Green
       return
    }
    
    $profileContent = gc $profilePath
   if($profilecontent |? { $_ -match "\#PSMODULE\:$PSModuleName(\.\d)*$" } )
   {     
     $u = $profileContent |% {	    
        if($_ -match "\#PSMODULE\:$PSModuleName(\.\d)*$")
        {
            $contentStart = $true
            # Remove package registration 
        } elseif(($_ -match "\#PSMODULE") -and $contentStart)
        {
            $contentStart = $false
            # Remove package registration
        } elseif($contentStart)
        {
           #Remove existing entries in the block
        } else {
            "$_"
        }
      } | Set-Content $profilePath
	    
      Write-Host "Completed." -ForegroundColor Green
   } else {
     Write-Host "Package $PSModuleName is not registered." -ForegroundColor Green 
     return
   }
}

function Unregister-PSModule
{
   param (
      [string] $modulename,
      [bool] $removeAll=$false     
   )
                            
   if(!($moduleLists = Get-Module $modulename))
   {
      Write-host "Can not find any modules. Check if module is loaded."
      return 
   }      
   
   if(!$removeAll)
   {
     $personal = new-object System.Management.Automation.Host.ChoiceDescription "Personal","Remove module in PowerShell current user profile"
     $global = new-object System.Management.Automation.Host.ChoiceDescription "Global","Remove module in PowerShell ALL user profile"
     $options = [System.Management.Automation.Host.ChoiceDescription[]]($personal,$global)
   
     $selectedStatus = $host.ui.PromptForChoice("New-PSModule UnRegistration","Do you want to unregister the module from profile?",$options,0)
   }
        	
   if (($selectedStatus -eq 0) -or $removeAll)
   {
      Write-Host "Removing from current user profile. $($moduleLists.Name) modules"
      Remove-Profile -profilePath $profile.CurrentUserAllHosts -PSModuleName $moduleLists.Name 
      Remove-Profile -profilePath $profile.CurrentUserCurrentHost -PSModuleName $moduleLists.Name  
                 
   } 
   if (($selectedStatus -eq 1) -or $removeAll)
   {
      Write-Host "Removing from all user profile. $($moduleLists.Name) modules"
      Remove-Profile -profilePath $profile.AllUsersAllHosts -PSModuleName $moduleLists.Name
      Remove-Profile -profilePath $profile.AllUsersCurrentHost -PSModuleName $moduleLists.Name   
   }
   
   $moduleLists |% {     
      if(get-module -name $_.Name)
      {
          remove-module -name $_.Name
          Write-Host "..Removing module $($_.Name)"
		  Write-Host "Unregistering $($_.Name) has been completed."
      }
   } 
   
}
